package com.syf.kit.core.ds.impl;

import cn.hutool.core.util.StrUtil;
import com.syf.kit.cont.RdsDSCont;
import com.syf.kit.enums.DataSourceType;
import com.syf.kit.enums.RDSType;

import javax.sql.DataSource;

public class SyfRWDataSource implements SyfDataSource {
    private final RDSType rdsType;

    private final DataSource rwDs;

    private final DataSource onlyReadDs;

    public SyfRWDataSource(RDSType rdsType, DataSource rwDs, DataSource onlyReadDs) {
        this.rdsType = rdsType;
        this.rwDs = rwDs;
        this.onlyReadDs = onlyReadDs;
    }

    @Override
    public RDSType getRDSType() {
        return rdsType;
    }

    @Override
    public DataSourceType getDsType() {
        return DataSourceType.RW;
    }

    @Override
    public DataSource getDefaultDataSource() {
        return onlyReadDs;
    }

    @Override
    public DataSource getDataSourceByName(String name) {
        if (StrUtil.equals(name, RdsDSCont.ONLY_READ_DS_NAME) || StrUtil.equals(name, RdsDSCont.DEFAULT_DS_NAME)) {
            return onlyReadDs;
        }

        if (StrUtil.equals(name, RdsDSCont.RW_DS_NAME)) {
            return rwDs;
        }

        throw new RuntimeException("数据源:" + name + "不存在，请仔细核对");
    }
}
